package org.openscheduler.scheduler;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.openscheduler.common.exception.SchedulerRuntimeException;
import org.quartz.Calendar;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.ListenerManager;
import org.quartz.Scheduler;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.quartz.SchedulerMetaData;
import org.quartz.Trigger;
import org.quartz.Trigger.TriggerState;
import org.quartz.TriggerKey;
import org.quartz.UnableToInterruptJobException;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.spi.JobFactory;

public class SchedulerWrapper {
	
	private Scheduler scheduler;
		public Scheduler getScheduler() {
			return scheduler;
		}
		

	public SchedulerWrapper(Scheduler scheduler) {
		this.scheduler = scheduler;
	}
	
	public SchedulerWrapper() {
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
		} catch (SchedulerException e) {
			throw new SchedulerRuntimeException(e);
		}
	}
	
	public SchedulerWrapper(String fileName) {
		try {
			StdSchedulerFactory factory = new StdSchedulerFactory(fileName);
			scheduler = factory.getScheduler();
		} catch (SchedulerException e) {
			throw new SchedulerRuntimeException(e);
		}
	}
	
	public SchedulerWrapper(Properties props) {
		try {
			StdSchedulerFactory factory = new StdSchedulerFactory(props);
			scheduler = factory.getScheduler();
		} catch (SchedulerException e) {
			throw new SchedulerRuntimeException(e);
		}
	}

	// ==============================================================
	// Wrap The org.quartz.Scheduler methods with "uncheck" exception
	// ==============================================================
	
	public String getSchedulerName() throws SchedulerException {
		return scheduler.getSchedulerName();
	}

	
	public String getSchedulerInstanceId() throws SchedulerException {
		return scheduler.getSchedulerInstanceId();
	}

	
	public SchedulerContext getContext() throws SchedulerException {
		return scheduler.getContext();
	}

	
	public void start() throws SchedulerException {
		scheduler.start();
	}

	
	public void startDelayed(int seconds) throws SchedulerException {
		scheduler.startDelayed(seconds); 
	}

	
	public boolean isStarted() throws SchedulerException {
		return scheduler.isStarted();
	}

	
	public void standby() throws SchedulerException {
		scheduler.standby();
	}

	
	public boolean isInStandbyMode() throws SchedulerException {
		return scheduler.isInStandbyMode();
	}

	
	public void shutdown() throws SchedulerException {
		scheduler.shutdown();
	}

	
	public void shutdown(boolean waitForJobsToComplete) throws SchedulerException {
		scheduler.shutdown(waitForJobsToComplete);
	}

	
	public boolean isShutdown() throws SchedulerException {
		return scheduler.isShutdown();
	}

	
	public SchedulerMetaData getMetaData() throws SchedulerException {
		return scheduler.getMetaData();
	}

	
	public List<JobExecutionContext> getCurrentlyExecutingJobs() throws SchedulerException {
		return scheduler.getCurrentlyExecutingJobs();
	}

	
	public void setJobFactory(JobFactory factory) throws SchedulerException {
		scheduler.setJobFactory(factory);
	}

	
	public ListenerManager getListenerManager() throws SchedulerException {
		return scheduler.getListenerManager();
	}

	
	public Date scheduleJob(JobDetail jobDetail, Trigger trigger) throws SchedulerException {
		return scheduler.scheduleJob(jobDetail, trigger);
	}

	
	public Date scheduleJob(Trigger trigger) throws SchedulerException {
		return scheduler.scheduleJob(trigger);
	}

	
	public void scheduleJobs(Map<JobDetail, List<Trigger>> triggersAndJobs, boolean replace) throws SchedulerException {
		scheduler.scheduleJobs(triggersAndJobs, replace);
	}

	
	public boolean unscheduleJob(TriggerKey triggerKey) throws SchedulerException {
		return scheduler.unscheduleJob(triggerKey);
	}

	
	public boolean unscheduleJobs(List<TriggerKey> triggerKeys) throws SchedulerException {
		return scheduler.unscheduleJobs(triggerKeys);
	}

	
	public Date rescheduleJob(TriggerKey triggerKey, Trigger newTrigger) throws SchedulerException {
		return scheduler.rescheduleJob(triggerKey, newTrigger);
	}

	
	public void addJob(JobDetail jobDetail, boolean replace) throws SchedulerException {
		scheduler.addJob(jobDetail, replace);
	}

	
	public boolean deleteJob(JobKey jobKey) throws SchedulerException {
		return scheduler.deleteJob(jobKey);
	}

	
	public boolean deleteJobs(List<JobKey> jobKeys) throws SchedulerException {
		return scheduler.deleteJobs(jobKeys);
	}

	
	public void triggerJob(JobKey jobKey) throws SchedulerException {
		scheduler.triggerJob(jobKey);
	}

	
	public void triggerJob(JobKey jobKey, JobDataMap data) throws SchedulerException {
		scheduler.triggerJob(jobKey, data);
	}

	
	public void pauseJob(JobKey jobKey) throws SchedulerException {
		scheduler.pauseJob(jobKey);
	}

	
	public void pauseJobs(GroupMatcher<JobKey> matcher) throws SchedulerException {
		scheduler.pauseJobs(matcher);
	}

	
	public void pauseTrigger(TriggerKey triggerKey) throws SchedulerException {
		scheduler.pauseTrigger(triggerKey);
	}

	
	public void pauseTriggers(GroupMatcher<TriggerKey> matcher) throws SchedulerException {
		scheduler.pauseTriggers(matcher);
	}

	
	public void resumeJob(JobKey jobKey) throws SchedulerException {
		scheduler.resumeJob(jobKey);
	}

	
	public void resumeJobs(GroupMatcher<JobKey> matcher) throws SchedulerException {
		scheduler.resumeJobs(matcher);
	}

	
	public void resumeTrigger(TriggerKey triggerKey) throws SchedulerException {
		scheduler.resumeTrigger(triggerKey);
	}

	
	public void resumeTriggers(GroupMatcher<TriggerKey> matcher) throws SchedulerException {
		scheduler.resumeTriggers(matcher);
	}

	
	public void pauseAll() throws SchedulerException {
		scheduler.pauseAll();
	}

	
	public void resumeAll() throws SchedulerException {
		scheduler.resumeAll();
	}

	
	public List<String> getJobGroupNames() throws SchedulerException {
		return scheduler.getJobGroupNames();
	}

	
	public Set<JobKey> getJobKeys(GroupMatcher<JobKey> matcher) throws SchedulerException {
		return scheduler.getJobKeys(matcher);
	}

	
	public List<? extends Trigger> getTriggersOfJob(JobKey jobKey) throws SchedulerException {
		return scheduler.getTriggersOfJob(jobKey);
	}

	
	public List<String> getTriggerGroupNames() throws SchedulerException {
		return scheduler.getTriggerGroupNames();
	}

	
	public Set<TriggerKey> getTriggerKeys(GroupMatcher<TriggerKey> matcher) throws SchedulerException {
		return scheduler.getTriggerKeys(matcher);
	}

	
	public Set<String> getPausedTriggerGroups() throws SchedulerException {
		return scheduler.getPausedTriggerGroups();
	}

	
	public JobDetail getJobDetail(JobKey jobKey) throws SchedulerException {
		return scheduler.getJobDetail(jobKey);
	}

	
	public Trigger getTrigger(TriggerKey triggerKey) throws SchedulerException {
		return scheduler.getTrigger(triggerKey);
	}

	
	public TriggerState getTriggerState(TriggerKey triggerKey) throws SchedulerException {
		return scheduler.getTriggerState(triggerKey);
	}

	
	public void addCalendar(String calName, Calendar calendar, boolean replace, boolean updateTriggers) throws SchedulerException {
		scheduler.addCalendar(calName, calendar, replace, updateTriggers);
	}

	
	public boolean deleteCalendar(String calName) throws SchedulerException {
		return scheduler.deleteCalendar(calName);
	}

	
	public Calendar getCalendar(String calName) throws SchedulerException {
		return scheduler.getCalendar(calName);
	}

	
	public List<String> getCalendarNames() throws SchedulerException {
		return scheduler.getCalendarNames();
	}

	
	public boolean interrupt(JobKey jobKey) throws UnableToInterruptJobException {
		return scheduler.interrupt(jobKey);
	}

	
	public boolean interrupt(String fireInstanceId) throws UnableToInterruptJobException {
		return scheduler.interrupt(fireInstanceId);
	}

	
	public boolean checkExists(JobKey jobKey) throws SchedulerException {
		return scheduler.checkExists(jobKey);
	}

	
	public boolean checkExists(TriggerKey triggerKey) throws SchedulerException {
		return scheduler.checkExists(triggerKey);
	}

	
	public void clear() throws SchedulerException {
		scheduler.clear();
	}
	
}
